<?php

namespace App\Http\Controllers\PhpCookBook\Number;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ExampleController extends Controller
{
    //
    public function isNumeric(){
        foreach ([5,'5',05,'05','5,100','12.3',12,3,' 16.7 ','five','0xDECAFBAD',0xDECAFBAD,'10e200'] as $item) {
            $isItNumeric = is_numeric($item);
            $actualType = gettype($item);
            print "Is the $actualType $item numeric ? ";
            if($isItNumeric){
                print 'yes';
            }else{
                print 'no';
            }
            print "\n";
        }
        var_dump(is_numeric('5,000'));
        var_dump(is_numeric(str_replace(',','','5,000')));
    }

    public function compareFloat(){
        $delta = 0.000001;
        $a = 1.00000000000;
        $b = 1.00000000001;
        if($a == $b){
            print "$a == $b";
        }else{
            print "$a != $b";
        }

        print "\n";

        if(abs($a - $b) < $delta){
            print "$a and $b are equal enough.";
        }else{
            print "$a and $b are no equal enough.";
        }
        print "\n";
    }

    public function round(){
        print 'round ' . round(5.4) ."\n";
        print 'round ' . round(5.5) ."\n";
        print '5.4 ceil ' . ceil(5.4) ."\n";
        print '5.5 ceil ' . ceil(5.5) ."\n";
        print '-5.4 ceil ' . ceil(-5.4) ."\n";
        print '-5.5 ceil ' . ceil(-5.5) ."\n";
        print 'floor ' . floor(5.4) ."\n";
        print 'floor ' . floor(5.5) ."\n";
        print '-5.4 floor ' . floor(-5.4) ."\n";
        print '-5.5 floor ' . floor(-5.5) ."\n";
    }

    public function range(){
        $number = range(3,7);//代码简洁,但大数组可能会不必要的占用过多的内存
        var_dump($number);
        $str = range('l','q');
        var_dump($str);
        $str = range('l',5);
        var_dump($str);
    }

    public function mtSrand(){
        mt_srand(34534);
        $first = $this->pickColor();
        $second = $this->pickColor();
        dd($first,$second);
    }

    private function pickColor(){
        $color= array('red','orange','yellow','blue','green','indigo','violet');
        $i = mt_rand(0,count($color)-1);
        return $color[$i];
    }

    public function randWeight(){
        $ads = ['ford' => 12234,
            'att' => 33424,
            'ibm' => 16823
        ];
        mt_srand(34534);
        $ad = $this->randWeighted($ads);
        $ad2 = $this->randWeighted($ads);
        dd($ad,$ad2);
    }

    //生成偏随机数
    private function randWeighted($numbers){
        $total = 0;
        foreach ($numbers as $number =>$weight){
            $total += $weight;
            $distribution[$number] = $total;
        }
        $rand = mt_rand(0,$total - 1);
        foreach ($distribution as $number => $weights) {
            if($rand < $weights) { return $number; };
        }
    }



    private  function incremental_total($numbers){
        $total = 0;
        foreach($numbers as $number => $weight){
            $total += $weight;
            yield $number => $total;
        }
    }

    private function rand_weighted_generator($numbers){
        $total= array_sum($numbers);
        $rand = mt_rand(0,$total - 1);
        foreach ($this->incremental_total($numbers) as $number => $weight){
            if ($rand < $weight) { return $number; }
        }
    }

    public function numberFormat(){
        $number = 31415.54534;
        list($int,$dec) = explode('.',$number);
        $usa = new \NumberFormatter('en-US',\NumberFormatter::DEFAULT_STYLE);
        $france = new \NumberFormatter('ch-CH',\NumberFormatter::SPELLOUT);
        $formatted[0] = number_format($number);//会四舍五入
        $formatted[1] = number_format($number,strlen($dec));//需要格式化又不丢失小数
        $formatted[2] = $usa->format($number);//需要格式化又不丢失小数
        $formatted[3] = $france->format($number);//需要格式化又不丢失小数

        dd($formatted);
    }


}
